package controllers

import (
	"strconv"
	"strings"

	"git.oschina.net/goxiang2/web/admin/models"
	"github.com/astaxie/beego"
	// "time"
)

type RoleController struct {
	beego.Controller
}

func (c *RoleController) Get() {
	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/login")
		return
	}

	c.Data["title"] = beego.AppConfig.String("apptitle")
	c.Data["username"] = userinfo.(*LoginInfo).Username
	c.Data["token"] = models.GetToken()

	user_permission := models.GetPermission(userinfo.(*LoginInfo).Username)
	if (user_permission & (1 << uint(models.ModelUser))) == 0 {
		c.Ctx.Redirect(302, "/index")
		return
	}
	data := getIndex(user_permission)
	for k, v := range data {
		c.Data[k] = v
	}

	roles, _ := models.GetRolelist()
	permission := models.GetPermissions()

	c.Data["permissions"] = permission
	c.Data["role"] = roles
	c.TplName = "role.ace"
}

func (c *RoleController) Post() {
	if !models.CheckToken(c.GetString("token")) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败，请重新刷新界面"}
		c.ServeJSON()
		return
	}

	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/login")
		return
	}

	if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
		c.ServeJSON()
		return
	}

	r := new(models.Role)
	r.Name = c.GetString("name")
	i, _ := strconv.Atoi(c.GetString("permission"))
	r.Permission = int64(i)

	_, err := models.AddRole(r)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
		c.ServeJSON()
		return
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "创建角色成功"}
	c.ServeJSON()

	go models.SaveLog(userinfo.(*LoginInfo).Username, "创建角色", &map[string]interface{}{"name": r.Name, "permission": i})
}

func (c *RoleController) Put() {
	if !models.CheckToken(c.GetString("token")) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败，请重新刷新界面"}
		c.ServeJSON()
		return
	}

	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/login")
		return
	}

	if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
		c.ServeJSON()
		return
	}

	id, _ := c.GetInt64("id")
	role := models.GetRoleById(id)
	if role == nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "角色[" + strconv.Itoa(int(id)) + "]不存在"}
		c.ServeJSON()
		return
	}
	if role.Id == 1 {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "该用户[" + role.Name + "]不能修改"}
		c.ServeJSON()
		return
	}

	r := new(models.Role)
	r.Id = role.Id
	r.Name = c.GetString("name")
	i, _ := strconv.Atoi(c.GetString("permission"))
	r.Permission = int64(i)

	_, err := models.UpdateRole(r)
	if err != nil {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": err}
		c.ServeJSON()
		return
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "更新角色成功"}
	c.ServeJSON()

	go models.SaveLog(userinfo.(*LoginInfo).Username, "修改角色", &map[string]interface{}{"name": r.Name, "permission": i})
}

func (c *RoleController) Delete() {
	if !models.CheckToken(c.GetString("token")) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "操作失败，请重新刷新界面"}
		c.ServeJSON()
		return
	}

	userinfo := c.GetSession("user")
	if userinfo == nil {
		c.Ctx.Redirect(302, "/login")
		return
	}

	if !models.CheckPermission(userinfo.(*LoginInfo).Username, models.ModelUser) {
		c.Data["json"] = &map[string]interface{}{"status": false, "info": "没有操作权限"}
		c.ServeJSON()
		return
	}

	var ids []int64
	str := c.GetString("ids")
	array := strings.Split(str, ",")
	for i := 0; i < len(array); i++ {
		id, _ := strconv.Atoi(array[i])
		role := models.GetRoleById(int64(id))
		if role == nil {
			c.Data["json"] = &map[string]interface{}{"status": false, "info": "角色[" + array[i] + "]不存在"}
			c.ServeJSON()
			return
		}
		if role.Id == 1 {
			c.Data["json"] = &map[string]interface{}{"status": false, "info": "该用户[" + role.Name + "]不能删除"}
			c.ServeJSON()
			return
		}
		ids = append(ids, role.Id)
	}

	for i := 0; i < len(ids); i++ {
		models.DelRoleById(ids[i])
	}

	c.Data["json"] = &map[string]interface{}{"status": true, "info": "删除角色成功"}
	c.ServeJSON()

	go models.SaveLog(userinfo.(*LoginInfo).Username, "删除角色", &map[string]interface{}{"ids": str})
}
